package org.phalanx.client.mapeditor.dialogs;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import org.phalanx.client.UI.DialogManager;
import org.phalanx.client.UI.PhalanxIconButton;
import org.phalanx.client.UI.swing.PhalanxButtonFactory;
import org.phalanx.client.UI.swing.listeners.pButtonListener;
import org.phalanx.client.UI.swing.listeners.pButtonPressed;
import org.phalanx.client.mapeditor.PhalanxMapEditorPanel;
import org.phalanx.client.mapeditor.PhalanxMapSizeSetPanel;

public class PhalanxMapResizeDialog extends JFrame implements pButtonListener, WindowListener{
	public static final String NAME = "MAP_RESIZE_DIALOG";
	private static final long serialVersionUID = 7411958869848148118L;
	private PhalanxMapEditorPanel masterPanel;
	private JPanel contextPanel;
	private PhalanxMapSizeSetPanel sizePanel;
	private JPanel buttonPanel;
	private PhalanxIconButton okButton;
	private PhalanxIconButton cancelButton;
	
	public PhalanxMapResizeDialog(PhalanxMapEditorPanel parentPanel) {
		this.masterPanel = parentPanel;
		initializePanels();
		initializeFrame();
		initializeComponents();
	}
	
	private void initializeComponents() {
		Integer mapL = masterPanel.getDisplayContainer().getMapViewObject().getMap().getMapLength();
		Integer mapW = masterPanel.getDisplayContainer().getMapViewObject().getMap().getMapWidth();
		
		sizePanel.setLengthField(mapL);
		sizePanel.setWidthField(mapW);
	}
	
	private void initializeFrame() {
		this.setSize(384,336);
		JFrame masterFrame = masterPanel.getMasterFrame();
		
		int pX = ((masterFrame.getWidth() + masterFrame.getLocation().x) + 
				masterFrame.getLocation().x) / 5;
		int pY = ((masterFrame.getHeight() + masterFrame.getLocation().y) + 
				masterFrame.getLocation().y) / 5;
		
		this.setLocation(pX, pY);
		
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.setContentPane(contextPanel);
		this.addWindowListener(this);
	}
	
	private void initializePanels() {
		contextPanel = new JPanel();
		contextPanel.setBackground(Color.black);
		contextPanel.setLayout(new GridBagLayout());
		
		sizePanel = new PhalanxMapSizeSetPanel();
		
		buttonPanel = new JPanel();
		buttonPanel.setBackground(Color.black);
		buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		
		TitledBorder sizeBorder = BorderFactory.createTitledBorder("Resize Map");
		sizeBorder.setTitleColor(Color.white);
		
		sizePanel.setBorder(sizeBorder);
		
		setSizeGrid();
		setButtonGrid();
	}
	
	private void setSizeGrid() {
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 0.5;
		c.weighty = 0.5;
		
		contextPanel.add(sizePanel, c);
	}
	
	private void setButtonGrid() {
		okButton = PhalanxButtonFactory.getButton(PhalanxButtonFactory.DEFAULT_OK_BUTTON);
		cancelButton = PhalanxButtonFactory.getButton(PhalanxButtonFactory.DEFAULT_CANCEL_BUTTON);
		buttonPanel.add(okButton);
		buttonPanel.add(cancelButton);
		
		okButton.addButtonListener(this);
		cancelButton.addButtonListener(this);
		
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 1;
		c.fill = GridBagConstraints.HORIZONTAL;
		
		contextPanel.add(buttonPanel, c);
	}

	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowClosing(WindowEvent arg0) {
		this.closeWindow();
		
	}

	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void actionPerformed(pButtonPressed p) {
		if (p.getComponent().equals(okButton)) {
			Integer length = Integer.valueOf(sizePanel.getLengthField().getText());
			Integer width = Integer.valueOf(sizePanel.getWidthField().getText());
			
			masterPanel.getDisplayContainer().getMapViewObject().getMap().setMapDimensions(length, width);
			masterPanel.getDisplayContainer().getMapViewObject().repaint();
			this.closeWindow();
		}
		else if (p.getComponent().equals(cancelButton)) {
			this.closeWindow();
		}

	}

	private void closeWindow() {
		DialogManager d = DialogManager.getInstance();
		
		d.removeDialog(NAME);
		
		this.dispose();
	}
}
